package modeles;

import donnees.dao.SalleDao;
import donnees.modele.Salle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

public class ModeleSalle extends DefaultComboBoxModel implements ComboBoxModel {

    private SalleDao salle;
    private List<Salle> conteneur;

    public ModeleSalle() {
        salle = new SalleDao();
        conteneur = new ArrayList<>();
        charger();
    }

    public void charger() {
        this.removeAllElements();
        conteneur.clear();
        //Vider le conteneur
        List result = null;
        try {
            result = salle.listerSalle();
            for (Iterator it = result.iterator(); it.hasNext();) {
                Salle s = (Salle) it.next();
                conteneur.add(s);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        } finally {
            this.setSelectedItem(conteneur.get(0).getNom());
        }
    }

    @Override
    public int getSize() {
        return conteneur.size();
    }

    @Override
    public Object getElementAt(int index) {
        return conteneur.get(index).getNom();
    }

    @Override
    public int getIndexOf(Object element) {
        return conteneur.indexOf(element);
    }
}
